package com.akk.platform.oem.business;

import com.akk.module.avs.entity.AvsYbCard;
import com.akk.module.avs.model.request.AvsYbCardRequest;
import com.akk.module.avs.service.AvsYbCardService;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.Map;

@Component
public class YbCardBuiness {

    @Autowired
    private AvsYbCardService cardService;

    @Autowired
    private PictureService pictureService;

    public RestResponse list(Query query, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        Map params = query.getSearch();
        params.put("appId", ShiroUtils.getAdmin().getAppId());
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AvsYbCard> list = cardService.findByParams(params);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    public RestResponse save(AvsYbCardRequest request,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        if (request.getImage() == null)
            return RestResponse.build("1","请上传图片",null);
        Map result = pictureService.uploadPicture(request.getImage());
        if (result.get("status").toString().equals("1"))
            return RestResponse.build("1","文件上传失败",null);
        request.setCardImage(result.get("url").toString());
        request.setAppId(ShiroUtils.getAdmin().getAppId());
        cardService.save(request,request.getTagIds());
        return RestResponse.build("200","success",null);
    }

    public RestResponse update(AvsYbCardRequest request,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        AvsYbCard card = cardService.findById(request.getCardId());
        if (card == null)
            return RestResponse.build("1","信息不存在",null);
        if (request.getImage() != null){
            Map result = pictureService.uploadPicture(request.getImage());
            if (result.get("status").toString().equals("1"))
                return RestResponse.build("1","文件上传失败",null);
            if (StringUtils.isEmpty(card.getCardImage()) && card.getCardImage().length() > 8)
                pictureService.removeFile(card.getCardImage());
            card.setCardImage(result.get("url").toString());
        }
        card.setCardDesc(request.getCardDesc());
        card.setCardLink(request.getCardLink());
        card.setCardName(request.getCardName());
        card.setStatus(request.getStatus());
        cardService.update(card,request.getTagIds());
        return RestResponse.build("200","success",null);
    }

    public RestResponse del(Long cardId){
        cardService.del(cardId);
        return RestResponse.build("200","success",null);
    }

    public RestResponse info(Long cardId){
        AvsYbCard card = cardService.findById(cardId);
        if (card == null)
            return RestResponse.build("1","信息不存在",null);
        return RestResponse.build(card);
    }

}
